/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package koor.action.student;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import koor.action.Action;
import koor.action.ActionResult;
import koor.entity.Student;
import koor.persistent.StudentDAO;
import koor.service.ServiceLocator;

/**
 * The
 * <code>StudentSaveAction</code> class represents action, used to save student
 * information
 *
 * @author Admin
 */
public class StudentSaveAction implements Action {

    @Override
    public String getName() {
        return "student.save";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {

        if ((request.getParameter("id") == null) || (request.getParameter("id").isEmpty())
                || (request.getParameter("firstname") == null) || (request.getParameter("firstname").isEmpty())
                || (request.getParameter("secondname") == null) || (request.getParameter("secondname").isEmpty())
                || (request.getParameter("card") == null) || (request.getParameter("card").isEmpty())) {
            request.getSession().setAttribute("fail", "Неверные данные");
            return ActionResult.redirect("student.list");
        }

        // if Save button have been pressed
        if (request.getParameter("saveButton") != null) {
            try {
                Long id = Long.parseLong(request.getParameter("id"));
                Student student = (id == -1) ? new Student() : ServiceLocator.getService(StudentDAO.class).findOne(id);

                // load data from request
                student.setFirstName(request.getParameter("firstname"));
                student.setSecondName(request.getParameter("secondname"));
                student.setNumberOfCard(request.getParameter("card"));
                if ((request.getParameter("patronymic") != null)){
                    student.setPatronymic(request.getParameter("patronymic"));
                } else {
                    student.setPatronymic("");
                }

                StudentDAO studentDAO = ServiceLocator.getService(StudentDAO.class);
                if (id != 0) {
                    studentDAO.update(student); // update
                    request.getSession().setAttribute("win", student.getFirstName() + " " + student.getSecondName() + " " + student.getPatronymic() + " был обновлен");
                } else {
                    studentDAO.save(student); // create
                    request.getSession().setAttribute("win", student.getFirstName() + " " + student.getSecondName() + " " + student.getPatronymic() + " был добавлен");
                }
            } catch (Exception e) {
                request.getSession().setAttribute("fail", "Не удалось сохранить");
            }
        } else {
            request.getSession().setAttribute("fail", "Не удалось сохранить");
        }

        return ActionResult.redirect("student.list");
    }
}
